import java.util.Date;


public class RocketAnalysisTool 
{
	public static final int headerWidth = 42;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{				
		int headerWidth = 42;
		Project project = null;
		RocketAnalysisTool rat = new RocketAnalysisTool();
		RocketHelper rocketHelper = new RocketHelper(headerWidth);
		
		RocketTools.writeHeader("Rocket Analysis Tool - Final Iteration", "~", "^", 
								headerWidth);
						
		project = displayMainMenu(project, rat, rocketHelper);		
		
		System.out.print("Use relative histogram? (Y/N): ");		
		String 	yesNoInput = RocketTools.readInput();
		
		if (yesNoInput.equalsIgnoreCase("n") || yesNoInput.equalsIgnoreCase("no"))
			rocketHelper.produceChart(project, false);
		else // Generates charts for the specified project
			rocketHelper.produceChart(project, true);		
		
		// Save using default fileName (the project name)
		rocketHelper.save(project);		
	}


	private static Project displayMainMenu(Project project, RocketAnalysisTool rat, RocketHelper rocketHelper) 
	{
		boolean invalid = true;
		String[] mainMenuOptions = {"New Project", "Open Project", "Exit" };
		
		while(invalid)
		{
			int selection = RocketTools.createMenuAndWait(mainMenuOptions);
			
			switch (selection)
			{
				case 0: // Create new project.
					invalid = false;
					project = createProject(rat, rocketHelper);
					break;
				case 1: // Load existing project
					invalid = false;
					project = rocketHelper.load();
					break;
				case 2: // Exit
					invalid = false;
					System.out.println("Rat closing.");
					System.exit(1);
					break;
			}
		}
		return project;
	}

	private static Project createProject(RocketAnalysisTool rat,
										RocketHelper rocketHelper) 	
	{
		Project project = rat.createNewProject();
		
		// Save using default fileName (the project name)
		rocketHelper.save(project);		
		
		String yesNoInput = null;
		while (true)
		{			
			int releaseNumber = project.generateReleaseSequenceNum();
			Version version = rocketHelper.newVersion(releaseNumber);
			rocketHelper.analyseVersion(version);
			
			// Add new version
			project.addVersion(version);
			System.out.print("Would you like to add another version to" +
								" this project? (Y/N): ");
			
			yesNoInput = RocketTools.readInput();
			
			if (yesNoInput.equalsIgnoreCase("n") || yesNoInput.equalsIgnoreCase("no"))
				break;		
		}
		return project;
	}


	/**
	 * Allows user to create project using console input. 
	 */
	public Project createNewProject()
	{
		Project project = new Project();
		
		Date startDate = null;
		String projectName = "", projectDescription = "", projectStartDate = "";
		 				
		// Obtain currentProject name from user
		RocketTools.writeHeader("New Project", headerWidth);		
		System.out.print("Enter the Project Name: ");
		projectName = RocketTools.readInput();
		
		// Obtain currentProject start date from user OR use current
		System.out.print("Enter the Start Date DD/MM/YYYY : ");
		projectStartDate = RocketTools.readInput(); //User may or may not enter the start date,it's optional
		startDate = RocketTools.processDate(projectStartDate);		
		
		// Obtain currentProject description (optional)
		System.out.print("Enter the Project Description : ");
		projectDescription = RocketTools.readInput();		
		
		// Set persistent data 
		project.setName(projectName);
		project.setStartDate(startDate);
		project.setDescription(projectDescription);
		
		return project;
	}
}
